﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace GameHungTrung
{
    public abstract class Animal
    {
        protected Product eggPrototype, shitPrototype;

        protected List<Product> listProduct;

        protected string _animalName;

        private My2DSprite _animalSprite;

        public My2DSprite AnimalSprite
        {
            get { return _animalSprite; }
            set { _animalSprite = value; }
        }

        private Vector2 updateVector;

        private double delta = 1;

        //SpriteFont font;

        public virtual double AnimalMoveLevel_1()
        {
            Random r = new Random();
            return 10 * r.NextDouble();
        }

        public virtual double AnimalMoveLevel_2()
        {
            Random r = new Random();
            return 12 * r.NextDouble();
        }

        public virtual double AnimalMoveLevel_3()
        {
            Random r = new Random();
            return 14 * r.NextDouble();
        }

        public virtual double AnimalMoveLevel_4()
        {
            Random r = new Random();
            return 16 * r.NextDouble();
        }

        public virtual double AnimalMoveLevel_5()
        {
            Random r = new Random();
            return 18 * r.NextDouble();
        }

        public virtual double AnimalMoveLevel_6()
        {
            Random r = new Random();
            return 20 * r.NextDouble();
        }

        public virtual Product Procedure(string label)
        {         
            switch (label)
            {
                case "Egg":
                    return eggPrototype.Clone(_animalSprite.Position,
                        new Vector2(_animalSprite.getWidthTexture(),_animalSprite.getHeightTexture()));
                   
                case "Shit":
                    return shitPrototype.Clone(_animalSprite.Position,
                        new Vector2(_animalSprite.getWidthTexture(), _animalSprite.getHeightTexture()));
            }
            return null;
        }

        public virtual void LoadContent(ContentManager content)
        {            
            Texture2D[] tmpTexture;
            Vector2 tmpVector;

            // ro
            tmpTexture = new Texture2D[1];
            tmpTexture[0] = content.Load<Texture2D>(Global.PATH_FILE_IMAGE + _animalName.ToLower());

            tmpVector = new Vector2(0, 50);

            _animalSprite = new My2DSprite(tmpTexture, tmpVector);

            // product
            listProduct = new List<Product>();
            eggPrototype = new Egg();
            eggPrototype.LoadContent(content);

            shitPrototype = new Shit();
            shitPrototype.LoadContent(content);

            //font = content.Load<SpriteFont>(@"Font\Score");
        }

        TimeSpan timeSpan = TimeSpan.FromMilliseconds(Global.TIME_PROCEDURE_PRODUCT);

        private Vector2 _posRegionBasket;
        private Vector2 _whRegionBasket;

        public void UpdateRegionBasket(Vector2 posRegion, Vector2 whRegion)
        {
            _posRegionBasket = posRegion;
            _whRegionBasket = whRegion;
        }

        public virtual void Update(GameTime gameTime)
        {
            float _speed = 0;
            switch (Global.LEVEL)
            {
                case 1:
                    _speed = (float)AnimalMoveLevel_1();
                    break;
                case 2:
                    _speed = (float)AnimalMoveLevel_2();
                    break;
                case 3:
                    _speed = (float)AnimalMoveLevel_3();
                    break;
                case 4:
                    _speed = (float)AnimalMoveLevel_4();
                    break;
                case 5:
                    _speed = (float)AnimalMoveLevel_5();
                    break;
                case 6:
                    _speed = (float)AnimalMoveLevel_6();
                    break;
            }

            updateVector = new Vector2(
                _animalSprite.Position.X + (float)(delta * _speed),
                _animalSprite.Position.Y);

            //updateVector = new Vector2(
            //    _animalSprite.Position.X + (float)delta*2.0f,
            //    _animalSprite.Position.Y);

            if (updateVector.X <= 0)
            {
                updateVector.X = 0;
                delta = delta * -1;
            }
            if (updateVector.X >= Global.SCREEN_WIDTH - _animalSprite.getWidthTexture())
            {
                updateVector.X = Global.SCREEN_WIDTH - _animalSprite.getWidthTexture();
                delta = delta * -1;
            }

            _animalSprite.UpdatePosition(updateVector);

            _animalSprite.Update(gameTime);

            // update product

            // check time 5" -> 1 product
            Random r = new Random();

            
            timeSpan -= gameTime.ElapsedGameTime;
            if (timeSpan <= TimeSpan.Zero)
            {
                // add list
                
                int x = r.Next(0, 4);
                if (x <= 1)
                    listProduct.Add(Procedure("Egg"));
                if (x > 1)
                    listProduct.Add(Procedure("Shit"));

                //int a = r.Next(1, 3);
                timeSpan = TimeSpan.FromMilliseconds(Global.TIME_PROCEDURE_PRODUCT);
            }

            int nProduct = listProduct.Count;

            for (int i = 0; i < nProduct; i++)
            {
                if (listProduct[i].IsEndFall == true)
                {
                    listProduct.RemoveAt(i);
                    nProduct--;
                    i--;
                }
            }

            for (int i = 0; i < listProduct.Count; i++)
            {
                listProduct[i].UpdateRegionBasket(_posRegionBasket, _whRegionBasket);
                listProduct[i].Update(gameTime);
            }

            if (Global.NUMBER_EGG >= Global.UP_LEVEL)
            {
                Global.LEVEL++;
                Global.UP_LEVEL += Global.UP_LEVEL;
                Global.MAX_ANIMAL+=2;
            }
            
            if (Global.NUMBER_HEART <= 0)
            {
                Global.GAME_STATE = Global.GAMESTATE_ENUM.GAME_OVER;
                Global.SCORE = Global.NUMBER_EGG;
                Global.IS_START_GAME = false;
                if (Global.NUMBER_EGG > Global.HIGHSCORE)
                    Global.HIGHSCORE = Global.NUMBER_EGG;
            }

            if (Global.NUMBER_SHIT >= 3)
            {
                Global.GAME_STATE = Global.GAMESTATE_ENUM.GAME_OVER;
                Global.SCORE = Global.NUMBER_EGG;
                Global.IS_START_GAME = false;
                if (Global.NUMBER_EGG > Global.HIGHSCORE)
                    Global.HIGHSCORE = Global.NUMBER_EGG;
            }


        }

        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            _animalSprite.Draw(gameTime, spriteBatch);

            // draw product
            for (int i = 0; i < listProduct.Count; i++)
            {
                listProduct[i].Draw(gameTime, spriteBatch);
            }

            //for(int i=0; i<listProduct.Count; i++)
            //spriteBatch.DrawString(font,
            //    "Speed fall: \n" + listProduct[i].MaxDelta.ToString(),
            //    new Vector2(100, 200 + i*50), Color.Red);
        }

        public abstract Animal Clone();

        public void Reset()
        {
            listProduct.RemoveRange(0, listProduct.Count);
            listProduct = new List<Product>();
            Global.LEVEL = 1;
            Global.UP_LEVEL = 10;
            Global.MAX_ANIMAL = 3;
        }
    }
}
